package com.fr.report.parameter.processor;

import com.fr.base.XMLable;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLReadable;
import com.fr.base.xml.XMLableReader;
import com.fr.report.parameter.Parameter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SQLQueryUnitProcessor extends AbstractUnitProcessor
  implements XMLable
{
  public static final String SELECT = "SELECT";
  public static final String FROM = "FROM";
  public static final String WHERE = "WHERE";
  public static final String GROUP_BY = "Group BY";
  public static final String HAVING = "HAVING";
  public static final String ORDER_BY = "ORDER BY";
  public static final String AND = "AND";
  public static final String OR = "OR";
  private String queryType = "WHERE";
  private List conditionList = new ArrayList();

  public Object process(Map paramMap)
  {
    String str1 = " ";
    int i = 0;
    for (int j = 0; j < this.conditionList.size(); ++j)
    {
      SQLQueryCondition localSQLQueryCondition = (SQLQueryCondition)this.conditionList.get(j);
      if (localSQLQueryCondition.getRight() instanceof Parameter)
      {
        Object localObject = paramMap.get(((Parameter)localSQLQueryCondition.getRight()).getName());
        if (localObject == null)
          break label203;
        if (localObject.toString().trim().length() == 0)
          break label203:
        String str2 = localSQLQueryCondition.getLikeStart();
        String str3 = localSQLQueryCondition.getLikeEnd();
        localSQLQueryCondition = new SQLQueryCondition(localSQLQueryCondition.getAppend(), localSQLQueryCondition.getLeft(), localSQLQueryCondition.getOperator(), localObject);
        localSQLQueryCondition.setLikeStart(str2);
        localSQLQueryCondition.setLikeEnd(str3);
      }
      if (i == 0)
      {
        str1 = str1 + this.queryType;
        localSQLQueryCondition.setAppend(null);
        i = 1;
      }
      str1 = str1 + " " + localSQLQueryCondition;
    }
    label203: return str1;
  }

  public String getQueryType()
  {
    return this.queryType;
  }

  public void setQueryType(String paramString)
  {
    this.queryType = paramString;
  }

  public List getConditionList()
  {
    return this.conditionList;
  }

  public void setConditionList(List paramList)
  {
    this.conditionList = paramList;
  }

  public Parameter[] getParameters()
  {
    ArrayList localArrayList = new ArrayList();
    for (int i = 0; i < this.conditionList.size(); ++i)
    {
      Object localObject = ((SQLQueryCondition)this.conditionList.get(i)).getRight();
      if (localObject instanceof Parameter)
        localArrayList.add(localObject);
    }
    return ((Parameter[])localArrayList.toArray(new Parameter[localArrayList.size()]));
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if ((paramXMLableReader.isChildNode()) && (paramXMLableReader.getTagName().equals("SQLConditionList")))
      paramXMLableReader.readXMLObject(new XMLReadable(this)
      {
        private final SQLQueryUnitProcessor this$0;

        public void readXML()
        {
          if ((paramXMLableReader.isChildNode()) && (paramXMLableReader.getTagName().equals("SQLQueryCondition")))
          {
            SQLQueryCondition localSQLQueryCondition = new SQLQueryCondition();
            paramXMLableReader.readXMLObject(localSQLQueryCondition);
            SQLQueryUnitProcessor.access$000(this.this$0).add(localSQLQueryCondition);
          }
        }
      });
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    if (this.conditionList.size() > 0)
    {
      paramXMLPrintWriter.startTAG("SQLConditionList");
      for (int i = 0; i < this.conditionList.size(); ++i)
        ((SQLQueryCondition)this.conditionList.get(i)).writeXML(paramXMLPrintWriter);
      paramXMLPrintWriter.end();
    }
  }

  static List access$000(SQLQueryUnitProcessor paramSQLQueryUnitProcessor)
  {
    return paramSQLQueryUnitProcessor.conditionList;
  }
}